<!-- 课程管理(模块) => 课程列表(菜单) => 台账操作列(签到记录按钮) => 点击路由跳转 => 展示此页面 -->
<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item :label="t('signLog.fields.classTime')" prop="startDate">
        <el-date-picker
          v-model="queryParams.classTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          :start-placeholder="t('common.startTimeText')"
          :end-placeholder="t('common.endTimeText')"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          {{ t('common.search') }}
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          {{ t('common.reset') }}
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <vxe-toolbar ref="toolbarRef" custom />
    <vxe-table
      id="iCourseSignLog-table"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
    >
      <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
      <vxe-column
        :title="t('signLog.fields.semester')"
        field="semester"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('signLog.fields.name')"
        field="name"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('signLog.fields.code')"
        field="code"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('signLog.fields.teacher')"
        field="teacher"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('signLog.fields.assistantCreate')"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      >
        <template #default="scope">
          <span v-if="scope.row.assistantCreate">{{ t('signLog.index.assistant') }}</span>
          <span v-else>{{ t('signLog.index.teacher') }}</span>
        </template>
      </vxe-column>
      <vxe-column
        :title="t('signLog.fields.classTime')"
        field="classTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('signLog.fields.endTime')"
        field="endTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('signLog.fields.studentNum')"
        field="studentNum"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      />
      <vxe-column
        :title="t('signLog.fields.signInNum')"
        field="signInNum"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      />
      <vxe-column
        :title="t('signLog.fields.notSignInNum')"
        field="notSignInNum"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      />
      <vxe-column
        :title="t('signLog.fields.beLateNum')"
        field="beLateNum"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      />
      <vxe-column
        :title="t('signLog.fields.earlyLeaveNum')"
        field="earlyLeaveNum"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      />
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        fixed="right"
        width="180"
      >
        <template #default="scope">
          <el-button link type="primary" @click="handleViewDetail(scope.row)">
            {{ t('signLog.index.operate.viewStudentLog') }}
          </el-button>
          <el-button link type="primary" @click="handleExportDetail(scope.row)">
            {{ t('signLog.index.operate.exportStudentLog') }}
          </el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SignLogForm ref="formRef" @success="getList" />
  <ExcelImportForm
    ref="importFormRef"
    object-name="课程签到"
    upload-url="/course/sign-log/import"
    template-url="/course/sign-log/get-import-template"
    @success="getList"
  />
  <CourseSignStudentLog ref="courseSignStudentLogRef" />
</template>

<script setup lang="ts">
import { CourseSignLogListRespVO, SignLogApi } from '@/api/course/signlog'
import SignLogForm from './SignLogForm.vue'
import ExcelImportForm from '@/components/ExcelImportForm/index.vue'
import type { VxeTableInstance, VxeTablePropTypes, VxeToolbarInstance } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import { CourseApi } from '@/api/course/course'
import { useTagsViewStore } from '@/store/modules/tagsView'
import CourseSignStudentLog from '@/views/course/signstudentlog/index.vue'
import { SignStudentLogApi } from '@/api/course/signstudentlog'
import download from '@/utils/download'

/** 课程签到 列表 */
defineOptions({ name: 'CourseSignLog' })

const route = useRoute()
const router = useRouter() // 路由对象
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const tagsViewStore = useTagsViewStore()

const toolbarRef = ref<VxeToolbarInstance>()
const tableRef = ref<VxeTableInstance>()

const loading = ref(true) // 列表的加载中
const list = ref<CourseSignLogListRespVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  courseId: undefined,
  courseName: undefined,
  teacher: undefined,
  classTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

// 课程信息
const getCourseInfo = async () => {
  const course = await CourseApi.getCourse(courseId.value)
  tagsViewStore.updateVisitedView(
    Object.assign({}, route, { meta: { title: `${course.name}-签到记录` } })
  )
}

/** 查询列表 */
const getList = async () => {
  queryParams.courseId = courseId.value
  loading.value = true
  try {
    const data = await SignLogApi.listSignLog(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref<InstanceType<typeof SignLogForm>>()
const openForm = (type: string, id?: number) => {
  formRef.value?.open(type, id)
}

/** 导入 */
const importFormRef = ref<InstanceType<typeof ExcelImportForm>>()
const handleImport = () => {
  importFormRef.value?.open()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SignLogApi.deleteSignLog(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const courseSignStudentLogRef = ref<InstanceType<typeof CourseSignStudentLog>>()
// 查看签到明细
const handleViewDetail = (row: CourseSignLogListRespVO) => {
  courseSignStudentLogRef.value?.open(row)
}

// 导出签到明细
const handleExportDetail = async (row: CourseSignLogListRespVO) => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    loading.value = true
    const data = await SignStudentLogApi.exportSignStudentLog({ signLogId: row.signLogId })
    download.excel(data, `${row.name}-${row.code}-课程签到学生记录.xlsx`)
  } catch {
  } finally {
    loading.value = false
  }
}

const courseId = ref(0)
/** 初始化 **/
onMounted(() => {
  if (!route.params.courseId) {
    router.push('/course/course')
    return
  }
  courseId.value = Number(route.params.courseId)

  getCourseInfo()
  getList()

  const table = tableRef.value
  const toolbar = toolbarRef.value
  if (table && toolbar) {
    table.connect(toolbar)
  }
})
</script>

<style lang="scss" scoped>
:deep(.vxe-cell .el-button + .el-button) {
  margin-left: 0;
}
</style>
